package com.foreverwin.mes.so.controller;

import com.foreverwin.mes.core.base.FrontPage;
import com.foreverwin.mes.core.base.AjaxResult;
import com.foreverwin.mes.core.utils.CommonMethods;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.foreverwin.mes.so.service.RecipeParamService;
import com.foreverwin.mes.so.model.RecipeParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 *
 * @author Syngna
 * @since 2020-02-04
 */
@Controller
@RequestMapping("/recipeParams")
public class RecipeParamController {
    private final Logger logger = LoggerFactory.getLogger(RecipeParamController.class);

    @Autowired
    public RecipeParamService recipeParamService;

    /**
    * 根据id查询
    *
    * @param id 主键
    * @return
    */
    @ResponseBody
    @GetMapping("/{id:.+}")
    public AjaxResult getRecipeParamById(@PathVariable String id) {
        RecipeParam result;
        try {
            result = recipeParamService.selectById(id);
        } catch (Exception e) {
            logger.error("getRecipeParamById -=- {}", e.toString());
            return AjaxResult.error( e.getMessage() );
        }
        return AjaxResult.success(result);
    }

    /**
     * 查询所有数据
     *
     * @return
     */
    @ResponseBody
    @GetMapping("")
    public AjaxResult getRecipeParamList(RecipeParam recipeParam){
        List<RecipeParam> result;
        try {
            EntityWrapper<RecipeParam> EntityWrapper = new EntityWrapper<>();
            EntityWrapper.setEntity(recipeParam);
            result = recipeParamService.selectList(EntityWrapper);
        } catch (Exception e) {
            logger.error("getRecipeParamList -=- {}", e.toString());
            return AjaxResult.error( e.getMessage() );
        }
        return AjaxResult.success(result);
    }

    /**
     * 保存和修改公用
     * @param recipeParam  传递的实体
     * @return  null 失败  实体成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/_save")
    public AjaxResult recipeParamSave(@RequestBody RecipeParam recipeParam) {
        String site = CommonMethods.getSite();
        String user = CommonMethods.getUser();
        int count;
        try {
            count = recipeParamService.insertOrUpdate(recipeParam) ? 1 : 0;
        } catch (Exception e) {
            logger.error("recipeParamSave -=- {}", e.toString());
            return AjaxResult.error( e.getMessage() );
        }
        return AjaxResult.success(count == 0 ? null : recipeParam);
    }

    /**
     * 根据id删除对象
     * @param id  实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.DELETE, value = "/{id:.+}")
    public AjaxResult recipeParamDelete(@PathVariable("id") String id){
        int count;
        try {
            count = recipeParamService.deleteById(id) ? 1 : 0;
        } catch (Exception e) {
            logger.error("recipeParamDelete -=- {}", e.toString());
            return AjaxResult.error( e.getMessage() );
        }
        return AjaxResult.success("count: " + count);
    }

    /**
     * 批量删除对象
     * @param ids 实体集合ID
     * @return  0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/_batchDelete")
    public AjaxResult removeByIds(List<String> ids){
        int count;
        try {
            count = recipeParamService.deleteBatchIds(ids) ? 1 : 0;
        } catch (Exception e) {
            logger.error("recipeParamBatchDelete -=- {}", e.toString());
            return AjaxResult.error( e.getMessage() );
        }
        return AjaxResult.success("count: " + count);
    }
}